package com.qen.yanggao

import cn.hutool.core.date.DatePattern
import cn.hutool.core.date.DatePattern.NORM_DATE_FORMAT
import cn.hutool.core.date.DatePattern.NORM_DATE_PATTERN
import cn.hutool.core.date.DateUtil
import cn.hutool.core.lang.Console
import cn.hutool.db.Db
import cn.hutool.poi.excel.ExcelUtil
import java.util.Date
import kotlin.String

fun main() {
    val inTbale = ArrayList<String>()
    for (str in tableList) {
        inTbale.add("'${str}'")
    }
    val list = Db.use().query("select * from TABLES where table_schema in (${inTbale.joinToString(",")})")
    val rows = ArrayList<DataNum>()
    for (entity in list) {
        val schema = entity.getStr("table_schema")
        if (tableList.contains(schema)) {
            val num = entity.getInt("table_rows") ?: 0
            rows.add(
                DataNum(
                    kuCode = "utf8mb4",
                    tableChinese = entity.getStr("table_comment"),
                    tableEng = entity.getStr("table_name"),
//                    Kong = if (num == 0) "1" else "0",
                    Kong = if (num == 0) "是" else "否",
                    gl = "无",
                    num = "${num}",
                    tjsj = DatePattern.NORM_DATE_FORMAT.format(Date()),
                    zeng = "${num / 12}",
                    tableCommon = entity.getStr("table_comment"),
                )
            )
        }
    }
    outExcel(rows)
}

data class DataNum(
    val kuCode: String = "utf8mb4",
    val tableChinese: String = "",
    val tableEng: String = "",
    val Kong: String = "",
    val gl: String = "无",
    val num: String = "",
    val tjsj: String = DatePattern.NORM_DATE_FORMAT.format(Date()),
    val zeng: String = "0",
    val tableCommon: String = ""
)

fun outExcel(rows: ArrayList<DataNum>) {
    val writer = ExcelUtil.getWriter("${OutPath}4数据表登记导入模板Sheet1.xlsx")
    writer.addHeaderAlias("kuCode", "数据库编码（必填）")
    writer.addHeaderAlias("tableChinese", "数据表中文名称（必填）")
    writer.addHeaderAlias("tableEng", "数据表英文名称（必填）")
    writer.addHeaderAlias("Kong", "是否空表（必填）(0:否 1：是)")
    writer.addHeaderAlias("gl", "关联其它表名称(没有填无)")
    writer.addHeaderAlias("num", "数据条数(注：填数字)（必填）")
    writer.addHeaderAlias("tjsj", "数据条数统计时间（格式：yyyy-MM-dd）（必填）")
    writer.addHeaderAlias("zeng", "数据每月增量(注：填数字)（必填）")
    writer.addHeaderAlias("tableCommon", "数据表描述（必填）")
    writer.write(rows, true)
    Console.log(writer.headerAlias)
    writer.close()
}